package com.lscl.rabbitmq;

import com.rabbitmq.client.*;
import java.io.IOException;

public class Consumer02_WorkQueues {

    public static void main(String[] args) throws Exception{
        // 创建连接工厂,用于获取频道channel
        ConnectionFactory factory=new ConnectionFactory();

        factory.setHost("192.168.40.132");
        factory.setPort(5672);
        factory.setUsername("lscl");
        factory.setPassword("admin");
        factory.setVirtualHost("/lscl");

        // 2.创建连接
        Connection connection = factory.newConnection();

        // 3.创建频道
        Channel channel = connection.createChannel();

        // 4.创建队列
        channel.queueDeclare("work_queues",true,false,false,null);

        // 5. 接收消息
        channel.basicConsume("work_queues", true, new DefaultConsumer(channel){

            // 回调方法,当收到消息之后,会自动执行该方法
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("body："+new String(body));
            }
        });
        // 不释放资源,让rabbitmq一直监听
    }
}
